package com.anyikang.volunteer.sos.openapp;

import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    Button btWeChatScan;
    Button btAliPayScan;
    Button btAliPayQcode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    /**
     *
     */
    private void initView() {

        btWeChatScan = this.findViewById(R.id.btWeChatScan);
        btWeChatScan.setOnClickListener(this);

        btAliPayScan = this.findViewById(R.id.btAliPayScan);
        btAliPayScan.setOnClickListener(this);

        btAliPayQcode = this.findViewById(R.id.btAliPayQcode);
        btAliPayQcode.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.btWeChatScan:
                openWechatScan();
                break;

            case R.id.btAliPayScan:
                openAliPay(1);
                break;

            case R.id.btAliPayQcode:
                openAliPay(2);
                break;
        }

    }


    /**
     * 打开微信扫一扫
     */
    public void openWechatScan() {

        try {
            Intent intent = new Intent();
            intent.setComponent(new ComponentName("com.tencent.mm", "com.tencent.mm.ui.LauncherUI"));
            intent.putExtra("LauncherUI.From.Scaner.Shortcut", true);
            intent.setFlags(335544320);
            intent.setAction("android.intent.action.VIEW");
            startActivity(intent);

        } catch (Exception e) {

            e.printStackTrace();
        }
    }


    /**
     * 支付宝扫一扫
     */
    private void openAliPay(int type) {

        try {
            //利用Intent打开支付宝
            //支付宝跳过开启动画打开扫码和付款码的urlscheme分别是：
            //alipayqr://platformapi/startapp?saId=10000007
            //alipayqr://platformapi/startapp?saId=20000056
           // Uri uri = Uri.parse("alipayqr://platformapi/startapp?saId=20000056");fukuanma


            Uri uri = null;

            if(type == 1)  //支付宝扫一扫
            {
                uri = Uri.parse("alipayqr://platformapi/startapp?saId=10000007");
            }
            else if(type == 2) //付款码
            {
                 uri = Uri.parse("alipayqr://platformapi/startapp?saId=20000056");
               //  uri = Uri.parse("jdmoble://");
            }

            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
        } catch (Exception e) {
            //若无法正常跳转，在此进行错误处理
            Toast.makeText(this, "打开失败，请检查是否安装了支付宝", Toast.LENGTH_SHORT).show();
        }


    }
}
